package com.to.aboomy.bannersample.adapter;

import com.to.aboomy.banner.Banner;
import com.to.aboomy.banner.IndicatorView;
import com.to.aboomy.bannersample.ResourceTable;
import com.to.aboomy.bannersample.bean.BannerBean;
import com.to.aboomy.bannersample.bean.Pager2BannerBean;
import com.to.aboomy.bannersample.bean.TextBean;
import com.to.aboomy.bannersample.viewpager.creator.ImageHolderCreator;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xujianhong
 * @since 2021-07-21
 */
public class BannerAdapter extends BaseItemProvider {
    private List<Object> mData;
    private Context mContext;
    private Component componentHeaderView;
    public BannerAdapter(Context context){
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData != null && position >= 0 && position < mData.size() ? mData.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemComponentType(int position) {
        Object item = getItem(position);
        if(item instanceof BannerBean){
            BannerBean bean = (BannerBean) getItem(position);
            return bean.itemType;
        }else if(item instanceof Pager2BannerBean){
            Pager2BannerBean bean = (Pager2BannerBean) getItem(position);
            return bean.itemType;
        }else if(item instanceof TextBean){
            TextBean bean = (TextBean) getItem(position);
            return bean.itemType;
        }else{
            return -1;
        }
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        Object item = getItem(i);
        int type = getItemComponentType(i);
        Component root = null;
        switch (type) {
            case 1:
                BannerBean bannerBean = (BannerBean) item;
                root = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_item_view_banner, null, false);
                Banner itemView = (Banner) root.findComponentById(ResourceTable.Id_banner);

                IndicatorView indicatorView = new IndicatorView(mContext)
                        .setIndicatorColor(Color.BLACK)
                        .setIndicatorSelectorColor(Color.WHITE);
                itemView.setIndicator(indicatorView)
                        .setHolderCreator(new ImageHolderCreator());

                itemView.setPages(bannerBean.urls,itemView.getCurrentPager());
                Text itemText = (Text) root.findComponentById(ResourceTable.Id_text);
                itemText.setText("我是recyclerView的一个item");
            break;
            case 2:
                TextBean textBean = (TextBean) item;
                root = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_item_text, null, false);
                Text text2 = (Text) root.findComponentById(ResourceTable.Id_text);
                text2.setText(textBean.text);
                break;
            case 3:
                Pager2BannerBean pager2BannerBean = (Pager2BannerBean) item;
                root = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_item_view_pager2_banner, null, false);
                Banner banner3 = (Banner) root.findComponentById(ResourceTable.Id_banner);

                IndicatorView indicatorView3 = new IndicatorView(mContext)
                        .setIndicatorColor(Color.BLACK)
                        .setIndicatorSelectorColor(Color.WHITE);
                banner3.setIndicator(indicatorView3)
                        .setHolderCreator(new ImageHolderCreator());

                banner3.setPages(pager2BannerBean.urls,banner3.getCurrentPager());
                Text text3 = (Text) root.findComponentById(ResourceTable.Id_text);
                text3.setText("我是recyclerView的一个item");
                break;
            case -1:
                root = componentHeaderView;
                break;
            default:
                root = null;
                break;
        }
        return root;
    }

    public List<Object> getData() {
        return mData;
    }

    public void setData(List<Object> mData) {
        this.mData = mData;
    }

    public void addData(Object obj) {
        addData(obj, getCount() - 1);
    }

    public void addData(Object obj, int position) {
        if (mData == null) {
            mData = new ArrayList<>();
        }
        if (position >= 0 && position < getCount()) {
            mData.add(position, obj);
        } else {
            mData.add(obj);
        }
    }

    public void removeData(Object obj) {
        if (!isEmpty()) {
            mData.remove(obj);
        }
    }

    public void removeData(int position) {
        if (!isEmpty() && position >= 0 && position < getCount()) {
            mData.remove(position);
        }
    }

    public void clearData() {
        if (!isEmpty()) {
            mData = null;
        }
    }

    public void addHeaderView(Component component){
        this.componentHeaderView = component;
        addData(new Object(),0);
    }

    public boolean isEmpty() {
        return mData == null || mData.isEmpty();
    }
}
